import { Image, Space } from 'antd';
import Typography from '@/pages/OrderModule/common/components/Typography';
import { formatZjfImg } from '@/utils/utils.jsx';
import CommonTag from '@/pages/OrderModule/common/components/CommonTag/index.jsx';
import Style from './index.module.less';

export function useGoodsColumns() {

  const goodsColumns = [
    {
      title: '系统订单号',
      dataIndex: 'sn',
      width: 130,
      render(value, record) {
        const dtk = record['goodsList'].map(v => v['refundStatus']).includes(1);
        const ytk = record['goodsList'].map(v => v['refundStatus']).includes(2);;
        const yuncang = record['logisticsList'].map(v => v['logisticsNoSrc']).includes(1);
        const arr = [
          //
          "source",//"手工订单",
          "merge",//"是否合单",
          "split",//"是否拆单",
          "weightSplit",//"是否重量拆单",
          "runVolume",//"是否跑量订单",
          "freightIntercept",//"是否运费拦截",
          "splitBoxState",//"是否拆分装箱",
          "afterSales",//"是否售后",
          "yituikuan",//"已退款",
          "daituikuan",//"待退款",
          "importYuncang",//已导入云仓运单
        ]
          .reduce((prev, key) => {
            if (key === "daituikuan") {
              dtk && prev.push("daituikuan");
            } else if (key === "yituikuan") {
              ytk && prev.push("yituikuan");
            } else if (key === "importYuncang") {
              yuncang && prev.push("importYuncang");
            } else if (key === "source") {
              const sourceConfig = {
                '-1': 'shougongpop',
                '0': 'syncOrder',
                '1': 'bufa',
                '2': 'huanhuo',
              }
              prev.push(sourceConfig[record[key]]);
            } else if (key === "freightIntercept") {
              const freightInterceptMap = {
                1: "alreadyFreightIntercept",
                2: "cancelFreightIntercept"
              };
              prev.push(freightInterceptMap[record[key]]);
            } else if (record[key]) {
              const keyMap = {
                'merge': "hebingpop",//是否合单
                'runVolume': "paoliangpop",//是否跑量订单
                'split': "chaifenpop",//是否拆单
                'splitBoxState': "wmsSplitOrder",//是否拆分装箱
                'afterSales': "shouhou",//是否售后
                'weightSplit': "chaifenpop"//是否重量拆单
              };
              if (!prev.includes(keyMap[key])) {
                prev.push(keyMap[key]);
              }
            }
            return prev;
          }, [])
        return (
          <>
            <Typography as="s1" style={{ cursor: 'pointer' }}>
              {value}
            </Typography>
            {record.mainOrder ? <div className={Style.tag_main}>主订单</div> : ''}
            <Space size={2}>
              {
                arr.map((v, i) => (
                  <CommonTag key={i} type={v} />
                ))}
            </Space>
          </>
        );
      },
    },
    {
      title: '店铺订单号',
      dataIndex: 'outOrderSnList',
      width: 130,
      render(value, record) {
        return (
          <>
            <Typography as="s1">{value || record.sn}</Typography>
            {/* <CommonTag type="chaifenpop" /> */}
          </>
        );
      },
    },
    {
      title: '买家账号+店铺',
      dataIndex: 'ecStoreName',
      width: 120,
      render(value, { buyerNickName }) {
        return <>
        <p>{buyerNickName}</p>
        <Typography as="s1">{value}</Typography>
        </>;
      },
    },
    {
      title: '收件地址',
      dataIndex: 'address',
      render(value, record) {
        let addressStr = '';
        // if(!addressStr){
         const { province, city, district, address, name, phone } = record.deliveryInfoList.filter(info => info.type === 2)?.[0] || {};
         addressStr = `${province || ''}${city || ''}${district || ''}${address || ''} ${name || ''}${phone || ''}`
        // }
        return <Typography as="s1">{addressStr}</Typography>;
      },
    },
    {
      title: '订单状态',
      dataIndex: 'orderStateDesc',
      width:85,
      render(value, record) {
        const orderStateMap = {//订单状态映射
          10: "待审核",
          20: "待发货",
          30: "异常订单",
          40: "已发货",
          50: "已取消"
        };
        return <Typography as="s1">{value || orderStateMap[record.state]}</Typography>;
      },
    },
    {
      // title: (
      //   <>
      //     <Typography as="s2" useBold>
      //       商品
      //     </Typography>
      //     <Popover placement="bottom" content={<GoodHead initValues={imageShowConfig} onChange={setImageShowConfig} />} overlayClassName={Style.goods_pop}>
      //       <span className={Style.goods_icon}>
      //         <IconFont type="icona-lujing13" />
      //       </span>
      //     </Popover>
      //   </>
      // ),
      title:'商品',
      dataIndex: 'field6',
      width:160,
      render(value, record) {
        return <div style={{ display: 'flex', flexWrap: 'wrap', gap: 6 }} >
          {record.goodsList.map((item, index) => {
            const { sysGoodsImage, storeId, goodsNum } = item;
            return <div style={{ width: '64px', height: "64px", borderRadius: '6px', overflow: 'hidden', position: 'relative', marginTop: '4px' }} key={index}>
              <Image src={formatZjfImg(sysGoodsImage, storeId, 64)} style={{width:"100%"}}/>
              <div style={{ display: 'inline-block', zIndex: 1000, background: '#F03D29', color: 'pink', width: 20, height: 22, lineHeight: '22px', textAlign: 'center', position: "absolute", top: 0, right: 0 }}>{goodsNum}</div>
            </div>
          })}
        </div>
      },
    },
    {
      title: '重量',
      dataIndex: 'totalWeight',
      width:70,
      render(value, record) {
        return <Typography as="s1">{value}</Typography>;
      },
    },
    {
      title: '买家留言',
      dataIndex: 'buyerMessage',
      render(value, record) {
        return <Typography as="s1">{value || '-'}</Typography>;
      },
    },
    {
      title: '卖家备注',
      dataIndex: 'sellerMessage',
      render(value, record) {
        return <Typography as="s1">{value || '-'}</Typography>;
      },
    },
    {
      title: '发货仓',
      dataIndex: 'warehouseName',
      width:100,
      render(value, record) {
        return <Typography as="s1">{value}</Typography>;
      },
    },
  ];

  return {
    goodsColumns,
  };
}
